package de.arnoldmedia.mybookshelf.resources.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

/**
 * JAXB Annotation Description:
 * 
 * XmlRootElement(namespace = "namespace") Define the root element for a XML
 * tree XmlType(propOrder = { "field2", "field1",.. }) Allows to define the
 * order in which the fields are written in the XML file The schema type to
 * which the class is mapped can either be named or anonymous XmlElement(name =
 * "neuName") Define the XML element which will be used. Only need to be used if
 * the neuNeu is different then the JavaBeans Name XmlAccessorType sets default
 * field and property serializability. By default, JAXB serializes public fields
 * and properties. By setting @XmlAccessorType, the bean can choose to only
 * allow annotated fields to be serialized.
 * 
 * D:\aarnold\workspace\bookshelf> schemagen -d . -cp .\bin
 * de.arnoldmedia.mybookshelf.resources.model.Bookshelf Note: Writing
 * D:\aarnold\workspace\bookshelf\schema1.xsd Note: Writing
 * D:\aarnold\workspace\bookshelf\schema2.xsd D:\aarnold\workspace\bookshelf>xjc
 * -p de.arnoldmedia.mybookshelf.resources.model schema1.xsd parsing a schema...
 * compiling a schema... de\arnoldmedia\mybookshelf\resources\model\Book.java
 * de\arnoldmedia\mybookshelf\resources\model\Bookshelf.java
 * de\arnoldmedia\mybookshelf\resources\model\ObjectFactory.java
 * 
 * @see http://www.tutorials.de/forum/java/263489-jaxb-tutorial.html
 * @author aarnold
 * @version 1.0
 * @date 22.02.2009
 * 
 */
@XmlRootElement(namespace = "de.arnoldmedia.mybookshelf")
@XmlType(propOrder = { "lastChanged", "shelf" })
public class Bookshelf {

	private List<Book> shelf;
	private Date lastChanged;

	public Bookshelf() {
		this.shelf = new ArrayList<Book>();
	}

	public Bookshelf(List<Book> sf) {
	}

	public List<Book> getShelf() {
		return this.shelf;
	}

	public void setShelf(List<Book> sf) {
		this.shelf = sf;
	}

	public Integer getBookIdx(String isbn) {

		Integer idx = -1;
		for (Book bo : this.shelf) {
			if (bo.getIsbn().equalsIgnoreCase(isbn)) {
				idx = this.shelf.indexOf(bo);
				break;
			}// if
		}//for
		return idx;

	}// getBookIdx

	public Book getBook(String isbn) {

		Book bo = null;
		Integer idx = this.getBookIdx(isbn);
		if (idx >= 0)
			bo = this.shelf.get(this.getBookIdx(isbn));
		return bo;

	}// getBook

	public void setBook(Book bo) {

		Integer idx = this.getBookIdx(bo.getIsbn());

		if (idx >= 0) {
			this.shelf.set(idx, bo);
		} else {
			this.shelf.add(bo);
		}// if

		System.out.println("[isbn=" + bo.getIsbn() + "][idx=" + idx + "][size="
				+ this.shelf.size() + "]");

	}// setBook

	/**
	 * @return the lastChanged
	 */
	public Date getLastChanged() {
		return lastChanged;
	}

	/**
	 * @param lastChanged
	 *            the lastChanged to set
	 */
	public void setLastChanged(Date lastChanged) {
		this.lastChanged = lastChanged;
	}

}// class
